package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

public class AppResult<T> {
    private int code;

    // 描述信息
    private String message;

    // 具体的数据
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功的情况下
     * @return
     */
    //按照规定的枚举类型进行返回
    public static AppResult success(){
        AppResult appResult = new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
        return appResult;
    }
    //按照自定义的message进行返回
    public static AppResult success(String message){
        AppResult appResult = new AppResult(ResultCode.SUCCESS.getCode(), message);
        return appResult;
    }
    //按照规定的数据进行返回
    public static<T> AppResult<T> success(T data){
        AppResult appResult = new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
        return appResult;
    }
    //按照规定的数据以及message进行返回
    public static<T> AppResult<T> success(String message,T data){
        AppResult appResult = new AppResult(ResultCode.SUCCESS.getCode(), message,data);
        return appResult;
    }
    /**
     * 定义失败的返回格式
     */
    //失败
    public static AppResult fail(){
        AppResult appResult = new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        return appResult;
    }
    //自定义返回信息
    public static AppResult fail(String message){
        AppResult appResult = new AppResult(ResultCode.FAILED.getCode(),message);
        return appResult;
    }
    //自定义错误数据
    public static<T> AppResult<T> fail(T data){
        AppResult appResult = new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
        return appResult;
    }
    //错误的状态
    public static AppResult fail(ResultCode resultCode){
        AppResult appResult = new AppResult(resultCode.getCode(), resultCode.getMessage());
        return appResult;
    }
}
